#include <stdio.h>
#include <sys/time.h>
#include <sys/times.h>
#include <atmi.h>
#include <fml32.h>
#include <Usysfl32.h>
#include <tpadm.h>

#define	MAXSRV	32
#define	MAXSVC	32

#ifdef DEBUG
#define	STRDBG(x, y)	printf("%s:	%s\n", x, y)
#define	LNGDBG(x, y)	printf("%s:	%ld\n", x, y)
#endif

struct usrvinfo {
	char	name[32][MAXSRV];
	char	srvgrp[80][MAXSRV];
	long	srvid[MAXSRV];
	long	pid[MAXSRV];
	long	no;
};
typedef struct usrvinfo USRVINFO;

struct usvcinfo {
	char	name[32][MAXSRV];
	char	srvgrp[80][MAXSRV];
	long	srvid[MAXSRV];
	char	rqaddr[32][MAXSRV];
	long	no;
};
typedef struct usvcinfo USVCINFO;

FBFR32 	*sendbuf, *rcvbuf;
long	ta_error;
char	*ta_status;

main(int argc, char *argv[])
{
	TPINIT		*tpinitbuf;
	USRVINFO	srv[2], *psrv[2];
	USVCINFO	svc[MAXSVC], *psvc[MAXSVC];
	char		srvgrp[80], srvnm[32], svcnm[32][MAXSVC];
	long		st, et;
	int		i, sn = 0, c = 0, mp = 0;

	if((tpinitbuf=(TPINIT *)tpalloc("TPINIT", NULL, TPINITNEED(16))) == (TPINIT *) NULL) {
		printf("tpalloc error : %s\n",tpstrerror(tperrno));
		exit(1);
	}

	strcpy (tpinitbuf->cltname, "tpsysadm");
        if (tpinit(tpinitbuf) == -1) {
		printf("tpinit error : %s\n",tpstrerror(tperrno));
                exit(1);
        }

	if((sendbuf = (FBFR32 *)tpalloc("FML32",NULL,1024*4)) == NULL) {
		printf("tpalloc error : %s\n",tpstrerror(tperrno));
		exit(1);
	}
	if((rcvbuf = (FBFR32 *)tpalloc("FML32",NULL,1024*4)) == NULL) {
		printf("tpalloc error : %s\n",tpstrerror(tperrno));
		tpfree((char *)sendbuf);
		exit(1);
	}

	psrv[0] = &srv[0];
	memset(psrv[0]->name, 0x00, sizeof(psrv[0]->name[0]));
	strcpy(psrv[0]->name[0], "smpl_svr");
	if(get_srvinfo(psrv[0]) != 0) {
		printf("get_srvinfo error\n");
		tpfree((char *)sendbuf);
		tpfree((char *)rcvbuf);
		exit(1);
	}
	tpfree((char *)sendbuf);
	tpfree((char *)rcvbuf);
}

int
get_srvinfo(USRVINFO *pusrv)
{
	long    i, len;

	Finit32(sendbuf, Fsizeof32(sendbuf));
   	Finit32(rcvbuf, Fsizeof32(rcvbuf));
	Fchg32(sendbuf, TA_CLASS, 0, "T_SERVER", 0);
 	Fchg32(sendbuf, TA_OPERATION, 0, "GET", 0);
 	Fchg32(sendbuf, TA_SERVERNAME, 0, pusrv->name[0], 0);

	if(tpcall(".TMIB", (char *)sendbuf, 0, (char **)&rcvbuf, &len, TPNOTRAN|TPNOCHANGE|TPSIGRSTRT|TPNOTIME) == -1) {
		printf("tpcall failed: %s\n", tpstrerror(tperrno));
		if( tperrno == TPESVCFAIL ) {
			Fget32(rcvbuf, TA_ERROR, 0, (char *)&ta_error, 0);
			ta_status = Ffind32(rcvbuf, TA_STATUS, 0, 0);
			printf("failure: %ld, %s\n", ta_error, ta_status);
		}

		return(-1);
	}

	Fget32(rcvbuf, TA_OCCURS, 0, (char *)&pusrv->no, 0);
	for(i =0; i < pusrv->no; i++) {
		Fget32(rcvbuf, TA_SRVGRP, i, (char *)pusrv->srvgrp[i], 0);
		Fget32(rcvbuf, TA_SERVERNAME, i, (char *)pusrv->name[i], 0);
		Fget32(rcvbuf, TA_SRVID, i, (char *)&pusrv->srvid[i], 0);
		Fget32(rcvbuf, TA_PID, i, (char *)&pusrv->pid[i], 0);
#ifdef DEBUG
		STRDBG(">get_srvinfo", pusrv->srvgrp[i]);
		STRDBG(">get_srvinfo", pusrv->name[i]);
		LNGDBG(">get_srvinfo", pusrv->srvid[i]);
		LNGDBG(">get_srvinfo", pusrv->pid[i]);
#endif
	}
	
	return(0);
}

int
#if defined(__STDC__) || defined(__cplusplus)
get_svcinfo(USVCINFO *pusvc)
#else
get_svcinfo(pusvc)
USVCINFO        *pusvc;
#endif
{
	int i;
	long len;


	Finit32(sendbuf, Fsizeof32(sendbuf));
   	Finit32(rcvbuf, Fsizeof32(rcvbuf));
	Fchg32(sendbuf, TA_CLASS, 0, "T_SVCGRP", 0);
 	Fchg32(sendbuf, TA_OPERATION, 0, "GET", 0);
 	Fchg32(sendbuf, TA_SERVICENAME, 0, pusvc->name[0], 0);

	if(tpcall(".TMIB", (char *)sendbuf, 0, (char **)&rcvbuf, &len, TPNOTRAN|TPNOCHANGE|TPSIGRSTRT|TPNOTIME) == -1) {
		printf("tpcall failed: %s\n", tpstrerror(tperrno));
		if( tperrno == TPESVCFAIL ) {
			Fget32(rcvbuf, TA_ERROR, 0, (char *)&ta_error, 0);
			ta_status = Ffind32(rcvbuf, TA_STATUS, 0, 0);
			printf("failure: %ld, %s\n", ta_error, ta_status);
		}

		return(-1);
	}

	Fget32(rcvbuf, TA_OCCURS, 0, (char *)&pusvc->no, 0);
	for(i =0; i < pusvc->no; i++) {
		Fget32(rcvbuf, TA_SRVGRP, i, (char *)pusvc->srvgrp[i], 0);
		Fget32(rcvbuf, TA_SRVID, i, (char *)&pusvc->srvid[i], 0);
		Fget32(rcvbuf, TA_RQADDR, i, (char *)pusvc->rqaddr[i], 0);
		Fget32(rcvbuf, TA_SERVICENAME, i, (char *)pusvc->name[i], 0);
#ifdef DEBUG
                STRDBG(">get_svcinfo", pusvc->srvgrp[i]);
                STRDBG(">get_svcinfo", pusvc->rqaddr[i]);
                LNGDBG(">get_svcinfo", pusvc->srvid[i]);
                STRDBG(">get_svcinfo", pusvc->name[i]);
#endif
	}


	return(0);
}

int
#if defined(__STDC__) || defined(__cplusplus)
start_srv(USRVINFO *pusrv)
#else
start_srv(pusrv)
USRVINFO        *pusrv;
#endif
{
	long i, len;


	Finit32(rcvbuf, Fsizeof32(rcvbuf));

	for(i=0; i<pusrv->no; i++) {
		Finit32(sendbuf, Fsizeof32(sendbuf));
		Fchg32(sendbuf, TA_CLASS, 0, "T_SERVER", 0);
		Fchg32(sendbuf, TA_OPERATION, 0, "SET", 0);
		Fchg32(sendbuf, TA_STATE, 0, "ACT", 0);
		Fchg32(sendbuf, TA_SRVGRP, 0, pusrv->srvgrp[i], 0);
		Fchg32(sendbuf, TA_SRVID, 0, (char *)&pusrv->srvid[i], 0);
#ifdef DEBUG
                STRDBG(">start_srv", pusrv->srvgrp[i]);
                LNGDBG(">start_srv", pusrv->srvid[i]);
#endif

		if(tpcall(".TMIB", (char *)sendbuf, 0, (char **)&rcvbuf, &len, TPNOTRAN|TPNOCHANGE|TPSIGRSTRT|TPNOTIME) < 0 ) {
			printf("tpcall failed: %s\n", tpstrerror(tperrno));
			if(tperrno == TPESVCFAIL) {
				Fget32(rcvbuf, TA_ERROR, 0, (char *)&ta_error, 0);
				ta_status = Ffind32(rcvbuf, TA_STATUS, 0, 0);
				printf("failure: %ld, %s\n", ta_error, ta_status);
			}
			return(-1);
		} 
	}

	return(0);
}

int
#if defined(__STDC__) || defined(__cplusplus)
stop_srv(USRVINFO *pusrv)
#else
stop_srv(pusrv)
USRVINFO        *pusrv;
#endif
{
	long i, len;


	Finit32(rcvbuf, Fsizeof32(rcvbuf));

	for(i=0; i<pusrv->no; i++) {
		Finit32(sendbuf, Fsizeof32(sendbuf));
		Fchg32(sendbuf, TA_CLASS, 0, "T_SERVER", 0);
		Fchg32(sendbuf, TA_OPERATION, 0, "SET", 0);
		Fchg32(sendbuf, TA_STATE, 0, "INA", 0);
		Fchg32(sendbuf, TA_SRVGRP, 0, pusrv->srvgrp[i], 0);
		Fchg32(sendbuf, TA_SRVID, 0, (char *)&pusrv->srvid[i], 0);
#ifdef DEBUG
                STRDBG(">stop_srv", pusrv->srvgrp[i]);
                LNGDBG(">stop_srv", pusrv->srvid[i]);
#endif
		if(tpcall(".TMIB", (char *)sendbuf, 0, (char **)&rcvbuf, &len, TPNOTRAN|TPNOCHANGE|TPSIGRSTRT|TPNOTIME) < 0 ) {
			printf("tpcall failed: %s\n", tpstrerror(tperrno));
			if(tperrno == TPESVCFAIL) {
				Fget32(rcvbuf, TA_ERROR, 0, (char *)&ta_error, 0);
				ta_status = Ffind32(rcvbuf, TA_STATUS, 0, 0);
				printf("failure: %ld, %s\n", ta_error, ta_status);
			}
			return(-1);
		} 
	}

	return(0);
}

int
#if defined(__STDC__) || defined(__cplusplus)
suspend_svc(USVCINFO *pusvc)
#else
suspend_svc(pusvc)
USVCINFO        *pusvc;
#endif
{
	long i, len;


	Finit32(rcvbuf, Fsizeof32(rcvbuf));

	for(i=0; i<pusvc->no; i++) {
		Finit32(sendbuf, Fsizeof32(sendbuf));
		Fchg32(sendbuf, TA_CLASS, 0, "T_SVCGRP", 0);
		Fchg32(sendbuf, TA_OPERATION, 0, "SET", 0);
		Fchg32(sendbuf, TA_STATE, 0, "SUS", 0);
		Fchg32(sendbuf, TA_SERVICENAME, 0, pusvc->name[i], 0);
		Fchg32(sendbuf, TA_SRVGRP, 0, pusvc->srvgrp[i], 0);
		Fchg32(sendbuf, TA_RQADDR, 0, pusvc->rqaddr[i], 0);
		Fchg32(sendbuf, TA_SRVID, 0, (char *)&pusvc->srvid[i], 0);
#ifdef DEBUG
                STRDBG(">suspend_svc", pusvc->name[i]);
                STRDBG(">suspend_svc", pusvc->srvgrp[i]);
                STRDBG(">suspend_svc", pusvc->rqaddr[i]);
                LNGDBG(">suspend_svc", pusvc->srvid[i]);
#endif
		if( tpcall(".TMIB", (char *)sendbuf, 0, (char **)&rcvbuf, &len , TPNOTRAN|TPNOCHANGE|TPSIGRSTRT|TPNOTIME) == -1) {
			printf("tpcall failed: %s\n", tpstrerror(tperrno));
			if(tperrno == TPESVCFAIL) {
				Fget32(rcvbuf, TA_ERROR, 0, (char *)&ta_error, 0);
				ta_status = Ffind32(rcvbuf, TA_STATUS, 0, 0);
				printf("failure: %ld, %s\n", ta_error, ta_status);
			}
			return(-1);
		}
	}

	return(0);
}

#if defined(__STDC__) || defined(__cplusplus)
usage(int argc, char *argv[])
#else
usage(argc, argv)
int argc;
char *argv[];
#endif
{
	printf("Usage: %s [-g srvgrp] [-s aout] [-c service]\n", argv[0]);
}
